package com.wanyang.service;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ArrayUtil;
import com.wanyang.bo.MemberBO;
import com.wanyang.dao.MemberLikeDao;
import com.wanyang.dao.PostCommentDao;
import com.wanyang.dao.PostDao;
import com.wanyang.dos.MemberLikeDO;
import com.wanyang.dos.MemberLikeDOExample;
import com.wanyang.mapper.MemberLikeDOMapper;
import com.wanyang.utils.DateUtil;
import com.wanyang.vo.MemberLikeGroupVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Service
@Slf4j
public class MemberLikeServiceImpl implements MemberLikeService {
    @Resource
    private MemberLikeDOMapper memberLikeDOMapper;

    @Resource
    private MemberLikeDao memberLikeDao;

    @Resource
    private PostCommentDao postCommentDao;

    @Resource
    private PostDao postDao;

    @Override
    public void likeOrUnlike(String type, Integer likedId) {
        if (ArrayUtil.contains(allowed_like_type, type)) {
            MemberBO memberBO = UserContext.get();
            Integer memberId = memberBO.getId();

            MemberLikeDOExample example = new MemberLikeDOExample();
            example.createCriteria()
                    .andMemberIdEqualTo(memberId)
                    .andLikedIdEqualTo(likedId)
                    .andLikedTypeEqualTo(type);

            MemberLikeDO memberLikeDO = memberLikeDOMapper.selectOneByExample(example);

            if (memberLikeDO != null) {
                Integer memberLikeId = memberLikeDO.getId();
                boolean likeStatus = memberLikeDO.getLikedStatus();

                memberLikeDO = new MemberLikeDO();
                memberLikeDO.setId(memberLikeId);
                memberLikeDO.setLikedStatus(!likeStatus);
                memberLikeDOMapper.updateByPrimaryKeySelective(memberLikeDO);

                // 更新点赞数量
                if (type.equals(like_type_post_comment)) {
                    postCommentDao.updateLikes(likedId, !likeStatus ? 1 : -1);

                } else if (type.equals(like_type_post)){
                    postDao.updateLikes(likedId, !likeStatus ? 1 : -1);
                }

            } else {
                memberLikeDO = new MemberLikeDO();
                memberLikeDO.setMemberId(memberId);
                memberLikeDO.setLikedId(likedId);
                memberLikeDO.setLikedStatus(true);
                memberLikeDO.setLikedType(type);
                memberLikeDO.setLikedTime(DateUtil.currentUnixTime());
                memberLikeDOMapper.insertSelective(memberLikeDO);

                // 更新点赞数量
                if (type.equals(like_type_post_comment)) {
                    postCommentDao.updateLikes(likedId, 1);

                } else if (type.equals(like_type_post)){
                    postDao.updateLikes(likedId, 1);
                }
            }
        } else {
            log.warn("Invalid like_type {}", type);

        }


    }

    @Override
    public List<Integer> queryLikes(Integer memberId, String type) {
        MemberLikeDOExample example = new MemberLikeDOExample();
        example.createCriteria()
                .andMemberIdEqualTo(memberId)
                .andLikedTypeEqualTo(type);

        List<MemberLikeDO> memberLikes = memberLikeDOMapper.selectByExample(example);

        return CollectionUtil.isNotEmpty(memberLikes) ?
                memberLikes.stream().map(MemberLikeDO::getLikedId).collect(Collectors.toList()) : new ArrayList<>();
    }

    @Override
    public List<Integer> queryLikes(Integer memberId, String type, List<Integer> targetIds) {
        MemberLikeDOExample example = new MemberLikeDOExample();
        example.createCriteria()
                .andMemberIdEqualTo(memberId)
                .andLikedStatusEqualTo(true)
                .andLikedTypeEqualTo(type)
                .andLikedIdIn(targetIds);

        List<MemberLikeDO> memberLikes = memberLikeDOMapper.selectByExample(example);

        return CollectionUtil.isNotEmpty(memberLikes) ?
                memberLikes.stream().map(MemberLikeDO::getLikedId).collect(Collectors.toList()) : new ArrayList<>();
    }

    @Override
    public List<MemberLikeGroupVO> queryLikesGroup(String type, List<Integer> targetIds) {
        return memberLikeDao.queryLikesGroup(type, targetIds);
    }
}
